<?php

namespace App\Listeners;

use App\Constant\QueueKey;
use App\Events\SmsSendEvent;
use App\Facades\CommonPlatformRequestFacade;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;

class SmsSendListener implements ShouldQueue
{


    public $connection = 'redis';

    public $queue = QueueKey::QUEUE_SMS;

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  SmsSendEvent  $event
     * @return void
     */
    public function handle(SmsSendEvent $event)
    {
      $res = CommonPlatformRequestFacade::smsQueueSend($event->phone_list,$event->detail);
      Log::debug('「发送短信」手机号为:'.json_encode($event->phone_list,JSON_UNESCAPED_UNICODE).'|内容为'.json_encode($event->detail,JSON_UNESCAPED_UNICODE).'，结果为：'.json_encode($res,JSON_UNESCAPED_UNICODE));
      if($res['code'] !== '01')
      {
          $phoneList = is_array($event->phone_list) ? implode('、',$event->phone_list): $event->phone_list;
          Log::warning('使用模板'.$event->detail['templateId'].'发送短信时发送失败，发送内容为'.json_encode($event->detail,JSON_UNESCAPED_UNICODE).',发送的手机号为'.$phoneList.'失败详情为:'.json_encode($res,JSON_UNESCAPED_UNICODE));
      }
    }
}
